package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.POJO.CheckGroup;
import com.itheima.POJO.CheckItem;
import com.itheima.POJO.Setmeal;
import org.apache.ibatis.annotations.*;

import java.util.HashMap;
import java.util.List;

public interface setmealMapper {

    @Select("select * from t_checkgroup")
    List<CheckGroup> findAllCheckgroup();

    //  @Insert("insert into t_setmeal values(null,#{name},#{code},#{helpCode},#{sex},#{age},#{price},#{remark},#{attention},#{img})")
    @Insert("insert into t_setmeal (code,name,sex,age,helpCode,price,remark,attention,img) values (#{code},#{name},#{sex},#{age},#{helpCode},#{price},#{remark},#{attention},#{img})")
    @SelectKey(keyProperty = "id", statement = "select last_insert_id();", resultType = int.class, before = false)
    void addSetmeal(Setmeal setmeal);

    @Insert("insert into t_setmeal_checkgroup (setmeal_id,checkgroup_id) values (#{setmeal_id},#{checkgroup_id})")
    void addSetmealAndcheckgroup(HashMap<String, Integer> map);


    @Select("<script>select * from t_setmeal " +
            "<if test=\"value != null and value.length > 0\"> " +
            "where code = #{value} or name like concat('%',#{value},'%') or helpCode like concat('%',#{value},'%') " +
            "</if>" +
            "</script>")
    Page<Setmeal> findPage(String queryString);


    @Delete("DELETE FROM t_setmeal_checkgroup WHERE setmeal_id = #{setmealId}")
    void deleteSetmeal_id4setmeal_checkgroup(@Param("setmealId") Integer setmealId);

    @Delete("DELETE FROM t_setmeal WHERE id=#{value}")
    void deleteSetmealById(Integer setmealId);

    @Select("select * from t_setmeal")
    List<Setmeal> findAllSetmeal();

    /*
      private Integer id;
    private String name;
    private String code;
    private String helpCode;
    private String sex;//套餐适用性别：0不限 1男 2女
    private String age;//套餐适用年龄
    private Float price;//套餐价格
    private String remark;
    private String attention;
    private String img;//套餐对应图片存储路径
    private List<CheckGroup> checkGroups;//体检套餐对应的检查组，多对多关系
     */
    @Select("select * from t_setmeal where id = #{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "code", column = "code"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "age", column = "age"),
            @Result(property = "price", column = "price"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "attention", column = "attention"),
            @Result(property = "img", column = "img"),
            @Result(property = "checkGroups", column = "id", javaType = List.class,
                    many = @Many(select = "com.itheima.dao.setmealMapper.findCehckgroupBySetmealID")
            )
    })
    Setmeal findSetmealByID(Integer id);

    /*
      private Integer id;//主键
    private String code;//编码
    private String name;//名称
    private String helpCode;//助记
    private String sex;//适用性别
    private String remark;//介绍
    private String attention;//注意事项
    private List<CheckItem> checkItems;//一个检查组合包含多个检查项
     */
    @Select("select * from t_checkgroup where id in(select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{cid})")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "code", column = "code"),
            @Result(property = "name", column = "name"),
            @Result(property = "helpCode", column = "helpCode"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "attention", column = "attention"),
            @Result(property = "checkItems", column = "id", javaType = List.class,
                    many = @Many(select = "com.itheima.dao.setmealMapper.findCheckItemByCheckgrouID")
            )
    })
    List<CheckGroup> findCehckgroupBySetmealID(Integer cid);

    /*
    private Integer id;//主键
    private String code;//项目编码
    private String name;//项目名称
    private String sex;//适用性别
    private String age;//适用年龄（范围），例如：20-50
    private Float price;//价格
    private String type;//检查项类型，分为检查和检验两种类型
    private String remark;//项目说明
    private String attention;//注意事项
    */
    @Select("SELECT * FROM t_checkitem WHERE id IN(SELECT checkitem_id FROM t_checkgroup_checkitem WHERE checkgroup_id = #{CIID})")
    List<CheckItem> findCheckItemByCheckgrouID(Integer CIID);

    @Select("SELECT COUNT(*) FROM t_order WHERE setmeal_id = #{id}")
    Integer findCountBySetmealID(Integer id);

    @Select("SELECT NAME FROM t_setmeal WHERE id = #{setmealId}")
    String findNameById(Integer setmealId);
}
